package com.hq.controller;

import com.hq.model.Root;
import com.hq.service.RootService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 登录
 */
@Controller
@RequestMapping("/back/login")
public class LoginController {
    @Autowired
    RootService rootService;

    @RequestMapping("/doLogin")
    String view(String username, String password, String role, HttpSession session, Model model){
        Subject subject = SecurityUtils.getSubject(); //获得主题
        UsernamePasswordToken token = new UsernamePasswordToken(username,password,role);
        try{
            subject.login(token);
            Root root = rootService.getUsername (username);
            session.setAttribute ("root",root);
            if (root.getRole ().equals ("管理员")){
                return "redirect:/back/hui/adminIndex";
            }else if (root.getRole ().equals ("游客")){
                return "redirect:/back/hui/passengerIndex";
            }else if (root.getRole ().equals ("导游")){
                return "redirect:/back/hui/guideIndex";
            }
        }catch (UnknownAccountException e){
            e.printStackTrace();
            model.addAttribute ("error","没有该用户");
            System.out.println("捕获到UnknownAccountException");
            return "back/login/view";
        }
        catch (AuthenticationException e){
            e.printStackTrace();
            model.addAttribute ("error","登录失败，请检查后重新登录");
            System.out.println("捕获到AuthenticationException");
            return "back/login/view"; //重定向回到登录页
        }
        return "redirect:/back/login/view";
    }

    @RequestMapping("/logout")
    String logout(HttpSession session){
        session.removeAttribute ("root");

        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/back/login/view";
    }

    @RequestMapping("/view")
    String login(){
        return "back/login/view";
    }

    //    注册
    @GetMapping("/register")
    public String register(){
        return "back/login/register";
    }

    @PostMapping("lll")
    public String lll( Root root,Model model){
        System.out.println(root.getUsername());
        if(root.getUsername().length() >= 3 && root.getUsername().length() < 17) {
            if (root.getPassword().length() > 3 && root.getPassword().length() < 17) {
                rootService.addRegister(root);
                return "redirect:/back/login/view";
            }else {
                model.addAttribute ("model","密码长度不符合（4-16）");
                return "back/login/register";
            }
        }else {
            model.addAttribute ("model","用户名长度不符合（3-16)");
            return "back/login/register";
        }
    }

}
